package com.danciyixue.boot.dcyx.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.danciyixue.boot.dcyx.mapper.WordsMapper;
import com.danciyixue.boot.dcyx.service.WordsService;
import com.danciyixue.boot.dcyx.model.entity.Words;
import com.danciyixue.boot.dcyx.model.form.WordsForm;
import com.danciyixue.boot.dcyx.model.query.WordsQuery;
import com.danciyixue.boot.dcyx.model.vo.WordsVO;
import com.danciyixue.boot.dcyx.converter.WordsConverter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 单词信息服务实现类
 *
 * @author youlaitech
 * @since 2025-06-15 16:23
 */
@Service
@DS("dcyx")
@RequiredArgsConstructor
public class WordsServiceImpl extends ServiceImpl<WordsMapper, Words> implements WordsService {

    private final WordsConverter wordsConverter;

    /**
    * 获取单词信息分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<WordsVO>} 单词信息分页列表
    */
    @Override
    public IPage<WordsVO> getWordsPage(WordsQuery queryParams) {
        Page<WordsVO> pageVO = this.baseMapper.getWordsPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取单词信息表单数据
     *
     * @param id 单词信息ID
     * @return 单词信息表单数据
     */
    @Override
    public WordsForm getWordsFormData(Long id) {
        Words entity = this.getById(id);
        return wordsConverter.toForm(entity);
    }
    
    /**
     * 新增单词信息
     *
     * @param formData 单词信息表单对象
     * @return 是否新增成功
     */
    @Override
    public boolean saveWords(WordsForm formData) {
        Words entity = wordsConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新单词信息
     *
     * @param id   单词信息ID
     * @param formData 单词信息表单对象
     * @return 是否修改成功
     */
    @Override
    public boolean updateWords(Long id,WordsForm formData) {
        Words entity = wordsConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除单词信息
     *
     * @param ids 单词信息ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteWordss(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的单词信息数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    /**
     * 随机获取指定教材的单词
     *
     * @param programId 教材ID
     * @param limit 获取数量
     * @return 单词列表
     */
    @Override
    public List<Words> getRandomWordsByProgramId(Long programId, Integer limit) {
        return this.baseMapper.getRandomWordsByProgramId(programId, limit);
    }

    /**
     * 随机获取指定教材的单词，排除指定的单词ID列表
     *
     * @param programId 教材ID
     * @param excludeIds 要排除的单词ID列表
     * @param limit 获取数量
     * @return 单词列表
     */
    @Override
    public List<Words> getRandomWordsByProgramIdExcluding(Long programId, List<Long> excludeIds, Integer limit) {
        return this.baseMapper.getRandomWordsByProgramIdExcluding(programId, excludeIds, limit);
    }

    /**
     * 根据单元ID获取所有单词
     *
     * @param unitId 单元ID
     * @return 单词列表
     */
    @Override
    public List<Words> getWordsByUnitId(Long unitId) {
        return this.baseMapper.getWordsByUnitId(unitId);
    }

    /**
     * 随机获取指定教材的单词，排除指定单元ID
     *
     * @param programId 教材ID
     * @param excludeUnitId 要排除的单元ID
     * @param limit 获取数量
     * @return 单词列表
     */
    @Override
    public List<Words> getRandomWordsByProgramIdExcludingUnit(Long programId, Long excludeUnitId, Integer limit) {
        return this.baseMapper.getRandomWordsByProgramIdExcludingUnit(programId, excludeUnitId, limit);
    }

    /**
     * 根据单词ID列表查询单词拼写
     *
     * @param wordIds 单词ID列表
     * @return 单词拼写列表
     */
    @Override
    public List<String> getWordSpellingsByIds(List<Long> wordIds) {
        if (wordIds == null || wordIds.isEmpty()) {
            return new ArrayList<>();
        }
        return this.baseMapper.getWordSpellingsByIds(wordIds);
    }

}
